package com.ruoyi.project.mq;

import com.ruoyi.project.constant.MqConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 张泽宇
 * 服务提供发送短信
 * @date 2020/10/13 16:46
 */
@Slf4j
@Component
public class DelaySender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public static DelaySender delaySender;

    @PostConstruct
    public void init() {
        delaySender = this;
        delaySender.rabbitTemplate = this.rabbitTemplate;
    }

    public static void send(String msg, int delayTime) {
        log.info("**delayTime：" + delayTime);
        delaySender.rabbitTemplate.convertAndSend(
                MqConstants.DEAD_LETTER_EXCHANGE,
                MqConstants.DELAY_ROUTING_KEY,
                msg,
                message -> {
                    message.getMessageProperties().setExpiration(String.valueOf(delayTime));
                    log.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " Delay1 sent.");
                    return message;
                });
    }
}
